package com.easyadmin.pro.common.initutils;

import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.modules.sys.entity.DictEntity;
import com.easyadmin.pro.modules.sys.entity.DictItemEntity;
import com.easyadmin.pro.modules.sys.service.IDictItemService;
import com.easyadmin.pro.modules.sys.service.IDictService;
import com.easyadmin.pro.modules.sys.vo.TableItemSchemaInfoVo;
import com.easyadmin.pro.modules.sys.vo.TableSchemaInfoVo;
import com.easyadmin.pro.tool.SnowflakeUtils;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * packageName com.easyadmin.pro.common.initutils
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className InitDictAndDictItem
 * @date 2024/8/20
 * @description 初始化字典数据
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InitDictAndDictItem {

    private final IDictService dictService;

    private final IDictItemService dictItemService;

    @Value("${easy-admin-pro.data-source-name}")
    private String dataSourceName;

    private static final String PARENTHESIS = "(";


    @PostConstruct
    public void initDict() {
        List<TableSchemaInfoVo> tableSchemaInfos = dictService.selectDictInformation(dataSourceName);
        if (!CollectionUtils.isEmpty(tableSchemaInfos)) {
            List<String> dictNames = tableSchemaInfos.stream()
                    .map(TableSchemaInfoVo::getDictName).collect(Collectors.toList());
            List<TableItemSchemaInfoVo> tableItemSchemaInfos = dictItemService
                    .selectDictItemInformation(dictNames, dataSourceName);
            Map<String, List<TableItemSchemaInfoVo>> itemInfoMaps = tableItemSchemaInfos.stream()
                    .collect(Collectors.groupingBy(TableItemSchemaInfoVo::getTableName));
            List<DictEntity> dictEntities = new ArrayList<>();
            List<DictItemEntity> dictItemEntities = new ArrayList<>();
            tableSchemaInfos.forEach(tableSchemaInfo -> {
                DictEntity dictEntity = new DictEntity();
                dictEntity.setId(SnowflakeUtils.generator()).setDictCode(tableSchemaInfo.getDictName())
                        .setStatus(Integer.valueOf(StatusConstants.STATUS_1))
                        .setDictName(tableSchemaInfo.getDictComment());
                List<TableItemSchemaInfoVo> itemInfos = itemInfoMaps.get(tableSchemaInfo.getDictName());
                if (!CollectionUtils.isEmpty(itemInfos)) {
                    itemInfos.forEach(itemInfo -> {
                        DictItemEntity dictItemEntity = new DictItemEntity();
                        dictItemEntity.setDictId(dictEntity.getId())
                                .setDictItemCode(itemInfo.getColumnName())
                                .setDictItemName(itemInfo.getColumnComment())
                                .setDictItemType(itemInfo.getDataType())
                                .setStatus(Integer.valueOf(StatusConstants.STATUS_1))
                                .setRequired(Objects.equals("YES", itemInfo.getIsNullable()) ? "0" : "1");
                        if (itemInfo.getColumnType().contains(PARENTHESIS)) {
                            String columnType = itemInfo.getColumnType();
                            String size = itemInfo.getColumnType()
                                    .substring(columnType.indexOf("(") + 1, columnType.lastIndexOf(")"));
                            dictItemEntity.setDictItemSize(Integer.valueOf(size));
                        }
                        dictItemEntities.add(dictItemEntity);
                    });

                }
                dictEntities.add(dictEntity);
            });

            if (!CollectionUtils.isEmpty(dictEntities)) {
                dictService.saveBatch(dictEntities);
            }

            if (!CollectionUtils.isEmpty(dictItemEntities)) {
                dictItemService.saveBatch(dictItemEntities);
            }
        }
    }
}